<%--
  Created by IntelliJ IDEA.
  User: 22328
  Date: 2021/2/6
  Time: 11:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <%--<!--[if lt IE 9]>--%>
    <%--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>--%>
    <%--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>--%>
    <%--<![endif]-->--%>
</head>

<body>
<div class="x-nav">
    <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a><cite>导航元素</cite></a></span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <div class="demoTable">
                        <div style="display: inline">
                            搜索ID：
                            <div class="layui-inline">
                                <input class="layui-input" name="id" id="demoReload_id" placeholder="请输入ID"
                                       autocomplete="off">
                            </div>
                        </div>
                        <div style="display: inline;margin-left: 15px">
                            用户名：
                            <div class="layui-inline">
                                <input class="layui-input" name="username" id="demoReload_username" placeholder="请输入用户名"
                                       autocomplete="off">
                            </div>
                        </div>
                        <div style="display: inline;margin-left: 15px">
                            性别：
                            <div class="layui-inline">
                                <select class="layui-select" name="sex" id="demoReload_sex" autocomplete="off"
                                        style="width: 200px">
                                    <option value="请选择">请选择</option>
                                    <option value="男">男</option>
                                    <option value="女">女</option>
                                </select>
                            </div>
                        </div>
                        <div style="display: inline;margin-left: 15px">
                            用户状态：
                            <div class="layui-inline">
                                <select class="layui-select" name="status" id="demoReload_status" autocomplete="off"
                                        style="width: 200px">
                                    <option value="请选择">请选择</option>
                                    <option value="1">启用</option>
                                    <option value="0">禁用</option>
                                </select>
                            </div>
                        </div>
                        <button class="layui-btn" data-type="reload"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                    <table class="layui-hide" id="LAY_table_admin" lay-filter="admin"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="barDemo">
             <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="detail"><i
            class="layui-icon layui-icon-form"></i></a>
             <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
             <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i
            class="layui-icon layui-icon-delete"></i> </a>
        </script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData"> 获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll"> 验证是否全选</button>
        <button class="layui-btn layui-btn-sm layui-bg-orange" lay-event="add">用户添加</button>
    </div>
</script>

<script src="./js/jquery-3.1.0.min.js" type="text/javascript"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use('table', function () {
        var table = layui.table;

        //方法级渲染
        table.render({
            elem: '#LAY_table_admin'
            , url: './getUsers?roleId=1'
            , limit: 9
            , limits: [7, 8, 9, 10, 15, 20, 30, 40]
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , layEvent: 'LAYTABLE_TIPS'
            , title: '管理员数据表'
            , cols: [[
                {checkbox: true, fixed: true, align: 'center', width: 80}
                , {field: 'id', title: 'ID', width: 80, sort: true, fixed: true, align: 'center'}
                , {field: 'userName', title: '用户名', align: 'center'}
                , {field: 'passWord', title: '密码', align: 'center'}
                , {field: 'sex', title: '性别', align: 'center'}
                , {field: 'age', title: '年龄', sort: true, align: 'center'}
                , {field: 'phone', title: '手机', align: 'center'}
                , {field: 'email', title: '邮箱', align: 'center'}
                , {
                    field: 'lastTime', type: 'date', title: '最近登录时间', align: 'center', sort: true,
                    templet: "<div>{{layui.util.toDateString(d.ordertime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                }
                , {field: 'status', title: '状态', align: 'center',templet:function(d){return statusToString(d.status)}}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 178, align: 'center'}
            ]]
            , id: 'testReload'
            , page: true
            // , height: 310
        });

        var $ = layui.$, active = {
            reload: function () {
                var id = $('#demoReload_id');
                var username = $("#demoReload_username");
                var sex = $("#demoReload_sex");
                var status = $("#demoReload_status");

                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        id: id.val(),
                        username: username.val(),
                        sex: sex.val(),
                        status: status.val()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //开启单元格监听事件
        table.on('tool()', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'detail':
                    xadmin.open('用户信息查看', './userInfoView?id=' + data.id);
                    break;
                case 'edit':
                    xadmin.open('用户信息编辑', './userEdit?id=' + data.id);
                    break;
                case 'del':
                    del(obj);
                    break;
            }
        });

        //头工具栏事件
        table.on('toolbar()',
            function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选' : '未全选');
                        break;
                    case 'add':
                        xadmin.open("用户添加", "./addUserHtml");
                        break;
                }
            });

    });

    function del(obj) {
        $.ajax({
            url: "./deleteUserById",
            type: "post",
            data: {id: obj.data.id},
            success: function (res) {
                if (res.data === "删除成功") {
                    layer.msg("删除成功");
                    obj.del();
                } else {
                    layer.msg("删除失败");
                }
            },
            error: function () {
                alert("404.html页面");
            }
        });
    }

    function statusToString(status) {
        if (status === 1){
            return '启用';
        } else {
            return '禁用';
        }
    }
</script>

</body>
</html>
